using System;
using System.Globalization;
using System.Linq;
using System.Windows.Data;
using CPlatePrintSys.Domain.Models;
using Newtonsoft.Json;

namespace CPlatePrintSys.Win.Converters
{
    /// <summary>
    /// 布局信息显示转换器
    /// </summary>
    public class LayoutInfoDisplayConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is string layoutInfoJson && !string.IsNullOrEmpty(layoutInfoJson))
            {
                try
                {
                    var layoutInfo = JsonConvert.DeserializeObject<LayoutInfo>(layoutInfoJson);
                    
                    if (layoutInfo.LayoutMean == "1" && layoutInfo.Mirrored != null)
                    {
                        // 镜像布局：显示原始对象和镜像对象编号
                        return $"{layoutInfo.Mirrored.OriginalObjNo}/{layoutInfo.Mirrored.MirrorObjNo}";
                    }
                    else if (layoutInfo.LayoutMean == "2" && layoutInfo.Merged != null)
                    {
                        // 合并布局：显示所有零件编号
                        var partNumbers = layoutInfo.Merged.PartLayouts.Select(p => p.SheetConsistentNo);
                        return string.Join("/", partNumbers);
                    }
                }
                catch (Exception ex)
                {
                    // 记录日志
                    Console.WriteLine($"解析布局信息失败: {ex.Message}");
                }
            }
            
            return string.Empty;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
} 